// 隐藏手机号
export function phoneSecret(phone) {
  if (phone && /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(phone)) {
    return phone.substring(0, 3) + '****' + phone.substring(7, 11)
  } else {
    return phone
  }
}
// 数值过大显示万
export function conversion(num) {
  return num > 9999 ? (num / 10000).toFixed(1) + '万' : num
}
// 获取当天日期遇0自动补齐
export function formatDateTime() {
  var myDate = new Date()
  var mydate = myDate.getDate()
  if (myDate.getDate() < 10) {
    mydate = '0' + myDate.getDate() //补齐
  }

  var month = myDate.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }

  var today = myDate.getFullYear() + '-' + month + '-' + mydate
  return today
}
export function formatMyDateTime(date) {
  var myDate = date || new Date()
  var day = myDate.getDate()
  let myMonth = myDate.getMonth() + 1
  if (day < 10) {
    day = '0' + day //补齐
  }
  if (myMonth < 10) {
    myMonth = '0' + myMonth
  }
  var today = myDate.getFullYear() + '-' + myMonth + '-' + day
  return today
}
//时间戳转格式 年月日
export function formatDateTime2(timeStamp) {
  var date = new Date()
  date.setTime(timeStamp * 1000)
  var y = date.getFullYear()
  var m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  var d = date.getDate()
  d = d < 10 ? '0' + d : d
  return y + '-' + m + '-' + d
}
// 获取时间范围
export function setTimeRange(v) {
  var now = new Date()
  var end = new Date()
  if (v == 1) {
    // 今天
    now.setDate(now.getDate())
    end.setDate(end.getDate())
  } else if (v == 2) {
    // 昨天
    now.setDate(now.getDate() - 1)
    end.setDate(end.getDate() - 1)
  } else if (v == 3) {
    // 本周
    // var nowDayOfWeek = now.getDay() //今天本周的第几天
    // if (nowDayOfWeek == 0) {
    //   nowDayOfWeek = 6 //周日
    // } else {
    //   nowDayOfWeek = nowDayOfWeek - 1
    // }
    // now.setDate(now.getDate() - nowDayOfWeek)
    // end.setDate(now.getDate() + 6)

    // const oneDay = 24 * 60 * 60 * 1000;
    // let nowDayOfWeek = now.getDay();
    // nowDayOfWeek = nowDayOfWeek === 0 ? 6 : nowDayOfWeek - 1;
    // now = new Date(now.getTime() - nowDayOfWeek * oneDay);
    // end = new Date(now.getTime() + 6 * oneDay);

    // 之前的本周逻辑计算有误  2025-4-2修改：
    const nowDayOfWeek = now.getDay(); // 0 (周日) ~ 6 (周六)
    const mondayOffset = nowDayOfWeek === 0 ? -6 : 1 - nowDayOfWeek;
    now.setDate(now.getDate() + mondayOffset); // 调整为本周一
    end = new Date(now); // 基于 now 创建 end
    end.setDate(end.getDate() + 6); // 设置为周日
  } else if (v == 4) {
    // 上周
    // var nowDayOfWeek = now.getDay() //今天本周的第几天
    // if (nowDayOfWeek == 0) {
    //   nowDayOfWeek = 6 //周日
    // } else {
    //   nowDayOfWeek = nowDayOfWeek - 1
    // }
    // now.setDate(now.getDate() - nowDayOfWeek - 7)
    // end.setDate(now.getDate() + 6)
    // 上周
    let value = Number(new Date())
    const one_day = 86400000 // 24 * 60 * 60 * 1000;
    const lastWeekDate = new Date(value - 7 * one_day)
    const day = lastWeekDate.getDay() === 0 ? 7 : lastWeekDate.getDay() // 返回1-7,7表示周日
    now = new Date(lastWeekDate.getTime() - (day - 1) * one_day)
    end = new Date(lastWeekDate.getTime() + (7 - day) * one_day)
  } else if (v == 5) {
    // 本月
    now.setDate(1)
    var nextMonth = end.getMonth() + 1
    var nextMonthFirstDay = new Date(end.getFullYear(), nextMonth, 1)
    var oneDay = 1000 * 60 * 60 * 24
    end = new Date(nextMonthFirstDay - oneDay)
  } else if (v == 6) {
    // 上月
    // var nextMonth = now.getMonth() - 1
    // now.setMonth(nextMonth)
    // now.setDate(1)
    // nextMonth = nextMonth + 1
    // var nextMonthFirstDay = new Date(end.getFullYear(), nextMonth, 1)
    // var oneDay = 1000 * 60 * 60 * 24
    // end = new Date(nextMonthFirstDay - oneDay)

    // 之前的上月逻辑计算有误  2025-3-31修改：
    const year = now.getFullYear();
    const month = now.getMonth();
    
    // 上个月的第一天 (直接构造日期避免溢出)
    now = new Date(year, month - 1, 1);
    
    // 上个月的最后一天 (本月第0天即上月最后一天)
    end = new Date(year, month, 0);
  } else if (v == 7) {
    //今年
    now.setMonth(0)
    now.setDate(1)
    end.setMonth(11)
    end.setDate(31)
  } else if (v == 8) {
    //去年
    now.setFullYear(now.getFullYear() - 1)
    now.setMonth(0)
    now.setDate(1)
    end.setFullYear(now.getFullYear())
    end.setMonth(11)
    end.setDate(31)
  }
  now.setHours(0)
  now.setMinutes(0)
  now.setSeconds(0)
  end.setHours(23)
  end.setMinutes(59)
  end.setSeconds(59) //我们在这里 输出一下
  let nowDate = {
    now: formatMyDateTime(new Date(now)),
    end: formatMyDateTime(new Date(end)),
  }
  return nowDate
}
//获取当前可视范围的高度
export function getClientHeight() {
  var clientHeight = 0
  if (document.body.clientHeight && document.documentElement.clientHeight) {
    clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight)
  } else {
    clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight)
  }
  return clientHeight
}
export var regExpObj = {
  //# 字符串匹配
  //是否为 数字！整数，浮点数
  isNum: function (num) {
    //# 是否为数组
    return !isNaN(num)
  },
  isEmail: function (mail) {
    //# 是否为 邮箱
    return /^([a-z0-9]+[_\-\.]?)*[a-z0-9]+@([a-z0-9]+[_\-\.]?)*[a-z0-9]+\.[a-z]{2,5}$/i.test(mail)
  },
  isIdCard: function (card) {
    //# 是否为 身份证
    return /^(\d{14}|\d{17})(\d|[xX])$/.test(card)
  },
  isMobile: function (mobile) {
    //# 是否为 手机
    return /^1[3456789]\d{9}$/.test(mobile)
  },
  isQQ: function (qq) {
    //# 是否为 QQ
    return /^[1-9]\d{4,10}$/.test(qq)
  },
  isTel: function (tel) {
    //# 是否为 电话
    return /^\d{3,4}-\d{7,8}(-\d{1,6})?$/.text(tel)
  },
  isUrl: function (url) {
    //# 是否为 URL
    return /https?:\/\/[a-z0-9\.\-]{1,255}\.[0-9a-z\-]{1,255}/i.test(url)
  },
  isColor: function (color) {
    //# 是否为 16进制颜色
    return /#([\da-f]{3}){1,2}$/i.test(color)
  },
  //@id ： 身份证 ，
  // @now : 当前时间 如：new Date('2013/12/12') , '2013/12/12'
  // @age ： 允许的年龄
  isAdult: function (id, allowAge, now) {
    //# 是否年龄是否成年
    var age = 0, // 用户 年月日
      nowDate = 0 //当前年月日
    allowAge = parseFloat(allowAge) || 18
    now = typeof now == 'string' ? new Date(now) : now || new Date()

    if (!this.isIdCard(id)) {
      return false
    }
    //15位身份证
    if (15 == id.length) {
      age = '19' + id.slice(6, 6)
    } else {
      age = id.slice(6, 14)
    }
    // 类型转换 整型
    age = ~~age
    nowDate = ~~Angela.date.format('YYYYMMDD', now)
    //比较年龄
    if (nowDate - age < allowAge * 1e4) {
      return false
    }
    return true
  },
  //浮点数
  isFloat: function (num) {
    //# 是否为 浮点数
    return /^(([1-9]\d*)|(\d+\.\d+)|0)$/.test(num)
  },
  //正整数
  isInt: function (num) {
    //# 是否为 正整数
    return /^[1-9]\d*$/.test(num)
  },
  //是否全为汉字
  isChinese: function (str) {
    //# 是否全为 汉字
    return /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])+$/gi.test(str)
  },
}

// localStorage数据存储
export const lcStorage = {
  set(key, value) {
    localStorage.setItem(key, JSON.stringify(value))
  },
  get(key) {
    return JSON.parse(localStorage.getItem(key))
  },
  remove(key) {
    localStorage.removeItem(key)
  },
}
// sessionStorage数据存储
export const seStorage = {
  set(key, value) {
    sessionStorage.setItem(key, JSON.stringify(value))
  },
  get(key) {
    return JSON.parse(sessionStorage.getItem(key))
  },
  remove(key) {
    sessionStorage.removeItem(key)
  },
}

